package cn.jwcat.service.impl;

import cn.jwcat.dealEntity.GoodsDto;
import cn.jwcat.entity.Goods;
import cn.jwcat.entity.Type;
import cn.jwcat.mapper.GoodsMapper;
import cn.jwcat.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 物资 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-21
 */
@Transactional
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

	@Override
	public List<GoodsDto> getAllInfo() {
		MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<>();
		wrapper
			.selectAll(Goods.class)
			.select(Type::getTypeName)
			.leftJoin(Type.class, Type::getTypeId, Goods::getTypeId)
			.orderByDesc(Goods::getCreateTime);
		return this.getBaseMapper().selectJoinList(GoodsDto.class, wrapper);
	}

	@Override
	public List<GoodsDto> postQuery(Type type) {
		MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<>();
		wrapper
			.selectAll(Goods.class)
			.select(Type::getTypeName)
			.leftJoin(Type.class, Type::getTypeId, Goods::getTypeId)
			.eq(Type::getTypeId, type.getTypeId())
			.orderByDesc(Goods::getCreateTime);
		return this.getBaseMapper().selectJoinList(GoodsDto.class, wrapper);
	}

	@Override
	public List<GoodsDto> getGoodsLastSix() {
		MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<>();
		wrapper
			.selectAll(Goods.class)
			.select(Type::getTypeName)
			.leftJoin(Type.class, Type::getTypeId, Goods::getTypeId)
			.last("limit 6")
			.orderByDesc(Goods::getNumber);
		return this.getBaseMapper().selectJoinList(GoodsDto.class, wrapper);
	}
}
